﻿/* 场景资源优化窗口和工具集合
 * 
 * 需要yaml in plugin 目录的引用
 */
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using YamlDotNet.RepresentationModel;
using System.IO;


public class BuiltScenesCheck : EditorWindow
{
    const string kCheckWindow = SBuildTool.kPreSTool + "检查场景";
    const string kTestYaml = SBuildTool.kPreSTool + "测试Read Yaml";
    const string kTestLandPrefab = SBuildTool.kPreSTool + "测试Read预制体";
    const string kTestScene = SBuildTool.kPreSTool + "测试Read场景";
    static Object _lightmapObj;
    [MenuItem(kTestScene, false)]
    public static void TestReadScene() {
        var path = Application.dataPath + "/Land_empty.unity";
        var scene = AssetReader.LoadYaml(path) as AssetScene;
        string asset_path = AssetDatabase.GUIDToAssetPath(scene.LightMapSetting.Guid);
        
        Debug.Log(AssetDatabase.LoadMainAssetAtPath(asset_path));
    }
    [MenuItem(kTestLandPrefab, false)]
    public static void TestReadPrefab() {
        var path = Application.dataPath + "/Plane.prefab";
        AssetReader.LoadYaml(path);
    }
    [MenuItem(kTestYaml,false)]
    public static void TestReadYaml()
    {
        var path =EditorBuildSettings.scenes[0].path;
        AssetReader.LoadYaml(path);
    }
    [MenuItem(kCheckWindow, false, 1101)]
    public static void ShowWindow()
    {
        EditorWindow win = GetWindow(typeof(BuiltScenesCheck));
        win.Show();
        //TODO:场景切换监控事件。。。。。。。

        //读取场景信息
        var path = SceneManager.GetActiveScene().path;
        var scene = AssetReader.LoadYaml(path) as AssetScene;
        string asset_path = AssetDatabase.GUIDToAssetPath(scene.LightMapSetting.Guid);

        if (!string.IsNullOrEmpty(asset_path))
        {
            _lightmapObj = AssetDatabase.LoadMainAssetAtPath(asset_path);
        }
        Debug.Log(AssetDatabase.LoadMainAssetAtPath(asset_path));
    }
    public string DataLightmapKey {
        get {
            return "Scene_Lightmap_" + SceneManager.GetActiveScene().path;
        }
    }
    private void OnDestroy()
    {
        _lightmapObj = null;
    }
    void DoSave2LogAndClear()
    {
        string pathOfLitmap = AssetDatabase.GetAssetPath(_lightmapObj);
        string guid = AssetDatabase.AssetPathToGUID(pathOfLitmap);
        EditorPrefs.SetString(DataLightmapKey,guid);
        
        Debug.Log("guid:"+EditorPrefs.GetString(DataLightmapKey));


        var pathYaml = SceneManager.GetActiveScene().path;
        YamlStream yaml = AssetReader.Load(pathYaml);

        pathYaml += "sss";//c测试用，保存目录
        //yaml.Save(new textstr());
        //yaml.Documents[0].a
        StreamWriter writer = new StreamWriter(pathYaml);
        //AssetReader.Set(yaml, 2, "m_LightingDataAsset", "0");
        //AssetReader.Set(yaml,)
        
        yaml.Documents[0].RootNode.Tag = "!u!104";
        yaml.Save(writer);
    }
    void DoRevert()
    { }
    private void OnGUI()
    {
        GUILayout.Label("概要===================");
        if (SceneManager.GetActiveScene() == null)
        {
            GUILayout.Label("！！！！不能是空（默认）场景");
                
            return;
        }
        GUILayout.Label(string.Format("当前场景：{0}", SceneManager.GetActiveScene().name));
        GUILayout.BeginHorizontal();
        GUILayout.Label("lightmap:");
        EditorGUILayout.ObjectField(_lightmapObj,typeof(UnityEngine.Object));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("记录并清空"))
        {
            if (EditorUtility.DisplayDialog("", "可能无法恢复，确认写到历史记录？", "确认", "取消"))
            {
                DoSave2LogAndClear();
            }
        }
        GUILayout.EndHorizontal();
        GUILayout.Label("历史记录资源===================");
        GUILayout.Label(string.Format("场景key:{0}", DataLightmapKey));
        GUILayout.Label(string.Format("GUID:{0}", EditorPrefs.GetString(DataLightmapKey)));//TODO:可能比较耗时
        if (GUILayout.Button("还原"))
        {
            DoRevert();
        }
        ////UnityEditor.lighting
        ////当前场景的？？？
        //foreach (var lit in LightmapSettings.lightmaps)
        //{
        //    LightmapData lightmap = null;
        //    //AssetDatabase.GetAssetOrScenePath(lightmap);
        //    //AssetDatabase.GetAssetPath(lit.);
        //    GUILayout.Label(lit.lightmapColor);
        //}

        //SceneAsset s;
        //s.
        
    }

    void OnLayout(string sceneName)
    {
        //EditorSceneManager scene = EditorSceneManager.get(sceneName);
        //scene.
       
    }
}
